package com.fin.zw.aiqas.service;

import com.fin.zw.aiqas.controller.ReconsiderController;
import com.fin.zw.aiqas.entity.InspectionDetail;
import com.fin.zw.aiqas.entity.UserInfo;
import com.fin.zw.aiqas.enums.ApproveStatus;
import com.fin.zw.aiqas.mapper.RuleStatisticsMapper;
import com.fin.zw.aiqas.model.data.InspectionDetailModel;
import com.fin.zw.aiqas.model.request.ReconsiderRequest;
import com.fin.zw.aiqas.model.request.ReconsiderationApprovalRequest;
import com.fin.zw.aiqas.model.session.SessionModel;
import com.fin.zw.aiqas.repository.InspectionDetailRepository;
import com.fin.zw.aiqas.repository.UserInfoRepository;
import com.fin.zw.aiqas.util.BadRequestException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service("reconsiderServer")
public class ReconsiderService {

    private final static Logger log = LoggerFactory.getLogger(ReconsiderController.class);


    @Autowired
    InspectionDetailRepository inspectionDetailRepository;//质检结果详情表

    @Autowired
    RuleStatisticsMapper ruleStatisticsMapper;

    @Autowired
    private UserInfoRepository userInfoRepository; //用户表


    /**
     * 发起复议申请
     *
     * @param request
     * @param session
     */
    public void addReconsideration(ReconsiderRequest request, SessionModel session) {
        log.info("发起复议申请......");
        InspectionDetail detail = inspectionDetailRepository.findById(request.getInspectionId()).orElseThrow(() -> new BadRequestException("", "质检结果详情未查询到"));
        detail.setReasons(request.getReasons());//申请意见
        detail.setApplyTime(new Date());//申请时间
        //申请人
        UserInfo user = userInfoRepository.findById(session.getUserId()).orElseThrow(() -> new BadRequestException("", "未查询到申请人!"));
        detail.setApplyUser(user);
        //复议状态
        detail.setReconsiderStatus(ApproveStatus.PENDING);
        inspectionDetailRepository.save(detail);
        log.info("复议申请完成!!!!");
    }


    /**
     * 查询复议申请的列表数据
     *
     * @param request
     * @return
     */
    public List<InspectionDetailModel> queryReconsiderationApproval(ReconsiderationApprovalRequest request) {
        return ruleStatisticsMapper.queryAllDetails(request);
    }
    public Integer queryCountReconsiderationApproval(ReconsiderationApprovalRequest request) {
        return ruleStatisticsMapper.queryCountAllDetails(request);
    }
}
